<?php

/**
 * Created by PhpStorm.
 * User: dumbledore
 * Date: 2018/3/7
 * Time: 下午3:24
 */
class Customer extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->library('form_validation');//加载表单验证类库
        $this->load->helper('json');
        $this->load->helper('url');
        $this->load->helper('common');
        $this->load->model('user_model');
        $this->load->model('customer_model');
        $this->load->model('admin_model');
        $this->load->model('bill_model');
        $this->load->library('session');
        $this->load->library('message');
    }

    // 申请成为账期用户页面
    public function admin_apply_for_period_user_index()
    {
        $post = $this->input->post();
        $data = '';
        $this->load->view('customer/admin_apply_for_period_user_index.php', $data);
    }

    // 修改账期资料页面
    public function admin_apply_for_period_user_edit_index()
    {
        $post = $this->input->post();
        $data = '';
        $customer_id = $this->session->customer_id;
        $account_period_apply = $this->customer_model->query_app_account_period_apply($customer_id);
        if ($account_period_apply['verify_status'] == 2 || $account_period_apply['verify_status'] == 0) {
            $data['company_info'] = $this->customer_model->query_app_company_info($customer_id);
            $data['account_period_apply'] = $account_period_apply;
            $this->load->view('customer/admin_apply_for_period_user_edit_index.php', $data);
        } else {
            redirect('index');
        }


    }

    // 客户发验证短信
    public function send_check_code()
    {
        $post = $this->input->post();
        $data['post'] = $post['tel'];
        if (isset($post['tel']) && $post['tel']) {
            $code = generate_code(6);
            $msg = "【文具1号企业站】您的验证码为" . $code . "，请勿向他人泄露验证码。如非本人操作，请忽略。";

            $res = $this->message->send_msg($post['tel'], $msg);

            if ($res['code'] == 0) {
                $this->message->add_msg_session($post['tel'], $code);
                send_json(array('code' => 0, 'data' => $code, 'msg' => "success"));
            } else {
                send_json(array('code' => -1, 'data' => '', 'msg' => $res['msg']));
            }
        } else {
            send_json(array('code' => -1, 'data' => '', 'msg' => "缺少参数"));
        }
    }

    //发送邮件验证码
    public function send_check_email()
    {
        send_json(array('code' => -1, 'data' => '', 'msg' => 555));
        $post = $this->input->post();

        if (check_param($post['email'])) {
            $sender_name = 666666;//发邮件人的地址
            $code = generate_code(6);//验证码
            $to_email = $post['email'];//收件人 email 地址
            $title = '你的验证码';// email 主题:
            $content = "【文具1号企业站】您的验证码为" . $code . "，请勿向他人泄露验证码。如非本人操作，请忽略。"; //email内容
            $res = $this->bill_model->send_email($sender_name, $to_email, $title, $content, $mailtype = 'html');//发送email

            if ($res == true) {
                $this->message->add_msg_session($post['email'], $code);
                log_message('error', '发送邮件成功，内容为：' . $content);
//                send_json(array('code'=>0,'data'=>'','msg'=>"success"));
            } else {
                log_message('error', '发送邮件失败，内容为：' . $content);
//                send_json(array('code'=>-1,'data'=>'','msg'=>$res['msg']));
            }

        } else {
            send_json(array('code' => -1, 'data' => '', 'msg' => "缺少参数"));
        }
    }

    // 申请成为账期用户
    public function admin_apply_for_period_user()
    {
        /**
         * name
         * province
         * city
         * district
         * address
         * business_license_pic_path
         * credit_code
         * business_license_number
         * type
         * number
         * industry
         * phone
         * contractor
         * email
         * tel
         * id_card_no
         * id_card_front_pic
         * id_card_front_pic_path
         * id_card_back_pic
         * id_card_back_pic_path
         * inviter_tel
         * minimum_purchase_amount
         * shipping_address
         * shipping_city
         * shipping_province
         * shipping_district
         * shipping_name
         * shipping_tel
         * shipping_name_second
         * shipping_tel_second
         * mark_invoice_type
         * invoice_type
         * invoice_company_name
         * invoice_code
         * invoice_address
         * invoice_tel
         * invoice_bank
         * invoice_account
         * invoice_normal_company_name
         * invoice_normal_code
         */
        $post = $this->input->post();

        if (check_param($post, ['name', 'province', 'city', 'district', 'address', 'business_license_pic_path', 'type', 'number', 'industry', 'phone',
            'contractor', 'email', 'code', 'tel', 'id_card_no', 'minimum_purchase_amount',
            'shipping_address', 'shipping_city', 'shipping_province', 'shipping_district', 'shipping_name', 'shipping_tel',
            'mark_invoice_type'])) {
            $customer_id = $this->session->customer_id;
            if ($post['invoice_type'] == 1) {
                // 需要开票，参数校验
                if (!check_param($post, ['invoice_company_name', 'invoice_code', 'invoice_address', 'invoice_tel', 'invoice_bank', 'invoice_account'])) {
                    send_json(array('code' => -1, 'data' => '', 'msg' => "增值税专用发票参数不全"));
                }
            } elseif ($post['invoice_type'] == 2) {
                if (!check_param($post, ['invoice_company_name', 'invoice_code'])) {
                    send_json(array('code' => -1, 'data' => '', 'msg' => "增值税普通发票参数不全"));
                } else {
                    $post['invoice_normal_company_name'] = $post['invoice_company_name'];
                    unset($post['invoice_company_name']);
                    $post['invoice_normal_code'] = $post['invoice_code'];
                    unset($post['invoice_code']);
                    unset($post['invoice_address']);
                    unset($post['invoice_tel']);
                    unset($post['invoice_bank']);
                    unset($post['invoice_account']);
                }
            } else {
                unset($post['invoice_company_name']);
                unset($post['invoice_code']);
                unset($post['invoice_address']);
                unset($post['invoice_tel']);
                unset($post['invoice_bank']);
                unset($post['invoice_account']);
            }
            // 参数校验
            $this->form_validation->set_rules('email', '邮箱', 'required|valid_email');
            $this->form_validation->set_rules('phone', '手机号码', 'required|valid_phone');

            // 短信校验
            $ck_res = $this->message->check_msg_session($post['tel'], $post['code']);
            if ($ck_res['code'] != 0) {
                send_json(array('code' => -1, 'data' => '', 'msg' => "验证码错误"));
            }

            $bool1 = $this->customer_model->save_app_company($customer_id, $post);
            $bool2 = $this->admin_model->save_admin_info($customer_id, ['account_period_status' => 1, 'prefix' => '']);
            $data3 = array('customer_id' => $customer_id, 'verify_status' => 0, 'type' => 1, 'created_at' => date('Y-m-d H:i:s', time()), 'updated_at' => date('Y-m-d H:i:s', time()));
            $bool3 = $this->db->insert("app_account_period_apply", $data3);
            $this->db->trans_begin();
            if ($bool1 && $bool2 && $bool3) {
                $this->db->trans_commit();
                send_json(array('code' => 0, 'data' => '', 'msg' => "保存成功"));
            } else {
                $this->db->trans_rollback();
                send_json(array('code' => -1, 'data' => '', 'msg' => "保存失败"));
            }

        } else {
            send_json(array('code' => -1, 'data' => '', 'msg' => "缺少参数"));
        }

    }

    // 修改前缀，只能改一次
    public function change_prefix()
    {
        $post = $this->input->post();
        $customer_id = $this->session->customer_id;
        $admin_info = $this->admin_model->query_admin_info($customer_id);
        if ($admin_info['prefix']) {
            send_json(array('code' => -1, 'data' => '', 'msg' => "前缀已经存在，不能再修改"));
        }

        // 默认为admin采购账号名称
        $username = $this->customer_model->query_customer_info($customer_id)->username;
        if (isset($post['prefix']) && $post['prefix']) {
            $prefix = $post['prefix'];
        } else {
            $prefix = $username;
        }
        $bool = $this->db->where(array("id" => $admin_info['id']))->update('app_admin', ["prefix" => $prefix]);
        if ($bool) {
            send_json(array('code' => 0, 'data' => '', 'msg' => "修改成功"));
        } else {
            send_json(array('code' => -1, 'data' => '', 'msg' => "修改失败"));
        }
    }

    // 查询admin账期用户申请的进度和信息
    public function query_admin_period_info()
    {
        $customer_id = $this->session->customer_id;
        $app_company_info = $this->db->query('select * from app_company WHERE customer_id = ' . $customer_id)->row_array();
        $account_period_info = $this->db->query('select * from app_admin WHERE customer_id = ' . $customer_id)->row_array();

        // 页面


    }

}